import {
	SET_USER_INFO,
	SET_USER_TOKEN,
	REMOVE_USER_INFO,
	SET_USER_CARTS_INFO,
	SET_USER_CARTS_INFO1,
	CHECKED_ALL,
	CHECKED_ALL1,
	CHECKED_ONE,
	CHECKED_ONE1,
	GOODS_NUM_ADD,
	GOODS_NUM_ADD1,
	GOODS_NUM_SUB,
	GOODS_NUM_SUB1,
	SET_TOKEN_PRICE,
	SET_WALLET_ADDRESS,
	SET_WALLET_PRIVATEKEY,
	SET_NFT_CONFIG
} from "./mutation-type";

export default {
	// 保存用户登录信息
	[SET_USER_INFO](state, userInfo) {
		state.userInfo = userInfo;
		uni.setStorageSync("userInfo", userInfo);
	},
	// 保存用户登录凭证
	[SET_USER_TOKEN](state, token) {
		state.token = token;
		uni.setStorageSync("token", token);
	},
	// 删除用户登录信息
	[REMOVE_USER_INFO](state) {
		uni.removeStorageSync("userInfo");
		uni.removeStorageSync("token");
    uni.removeStorageSync("walletAddress")
    uni.removeStorageSync("walletPrivatekey")
    uni.setStorageSync("needWalletPasswd",true)
    state.walletAddress = ""
    state.walletPrivateKey = ""
		state.userInfo = null;
		state.token = "";
		state.userCarts = [];
		state.totalGoods = 0;
		state.userCarts1 = [];
		state.totalGoods1 = 0;
	},
	// 保存购物车信息
	[SET_USER_CARTS_INFO](state, params) {
		console.log("<保存购物车信息>", params.supplierCartList);
		let num = 0;
		for (let item of params.supplierCartList) {
			for (let v of item.goods_list) {
				num += v.total_num * 1;
			}
		}
		// console.log(9999, num);
		state.totalGoods = num;
		state.userCarts = params.supplierCartList;
		state.userCarts1 = params.supplierCartList;
		state.cartList1 = params.cartList
		// console.log('state.totalGoods', state.totalGoods);
	},
	// 全选  非全选
	[CHECKED_ALL](state, checkInfo) {
		for (let item of state.userCarts) {
			if (item.id == checkInfo.name) {
				item.supplierChecked = checkInfo.value;
				// 选中的店铺下面的商品全部选中 否则为非选中
				item.goods_list.forEach((v) => {
					v.goodsChecked = checkInfo.value;
				});
				break;
			}
		}
	},
	// 单个商品选中
	[CHECKED_ONE](state, checkInfo) {
		console.log("单个商品选中", checkInfo);
		let goodsList = state.userCarts;
		for (let item of goodsList) {
			if (item.id == checkInfo.shopId) {
				for (let i = 0; i < item.goods_list.length; i++) {
					if (i == checkInfo.name) {
						item.goods_list[i].goodsChecked = checkInfo.value;
						break;
					}
				}
				for (let v of item.goods_list) {
					// 店铺勾选
					if (!v.goodsChecked) {
						item.supplierChecked = false;
						break;
					} else {
						item.supplierChecked = true;
					}
				}
			}
		}
	},
	// 单个商品 数量增加(默认增加1)
	[GOODS_NUM_ADD](state, checkInfo) {
		console.log("单个商品 数量增加(默认增加1)", checkInfo);
		for (let item of state.userCarts) {
			if (item.id == checkInfo.id) {
				for (let v of item.goods_list) {
					if (
						checkInfo.goods_id == v.goods_id &&
						checkInfo.goods_sku_id == v.goods_sku_id
					) {
						v.total_num = v.total_num * 1 + 1;
						break;
					}
				}
			}
		}
	},
	// 单个商品 数量减少(默认减少1)
	[GOODS_NUM_SUB](state, checkInfo) {
		console.log("单个商品 数量减少(默认减少1)", checkInfo);
		for (let item of state.userCarts) {
			if (item.id == checkInfo.id) {
				for (let v of item.goods_list) {
					if (
						checkInfo.goods_id == v.goods_id &&
						checkInfo.goods_sku_id == v.goods_sku_id
					) {
						v.total_num = v.total_num * 1 - 1;
						break;
					}
				}
			}
		}
	},
	// 保存购物车信息
	[SET_USER_CARTS_INFO1](state, params) {
		console.log("<保存购物车信息>", params);
		let num = 0;
		for (let item of params.supplierCartList) {
			for (let v of item.goods_list) {
				num += v.total_num * 1;
			}
		}
		// console.log(9999, num);
		state.totalGoods1 = num;
		state.userCarts1 = params.supplierCartList;
		// console.log('state.cartList1',params.cartList);
		state.cartList1 = params.cartList
		// console.log('state.totalGoods', state.totalGoods);
	},
	// 全选  非全选
	[CHECKED_ALL1](state, checkInfo) {
		for (let item of state.userCarts1) {
			if (item.id == checkInfo.name) {
				item.supplierChecked = checkInfo.value;
				// 选中的店铺下面的商品全部选中 否则为非选中
				item.goods_list.forEach((v) => {
					v.goodsChecked = checkInfo.value;
				});
				break;
			}
		}
	},
	// 单个商品选中
	[CHECKED_ONE1](state, checkInfo) {
		console.log("单个商品选中", checkInfo);
		let goodsList = state.userCarts1;
		for (let item of goodsList) {
			if (item.id == checkInfo.shopId) {
				for (let i = 0; i < item.goods_list.length; i++) {
					if (i == checkInfo.name) {
						item.goods_list[i].goodsChecked = checkInfo.value;
						break;
					}
				}
				for (let v of item.goods_list) {
					// 店铺勾选
					if (!v.goodsChecked) {
						item.supplierChecked = false;
						break;
					} else {
						item.supplierChecked = true;
					}
				}
			}
		}
	},
	// 单个商品 数量增加(默认增加1)
	[GOODS_NUM_ADD1](state, checkInfo) {
		console.log("单个商品 数量增加(默认增加1)", checkInfo);
		for (let item of state.userCarts1) {
			if (item.id == checkInfo.id) {
				for (let v of item.goods_list) {
					if (
						checkInfo.goods_id == v.goods_id &&
						checkInfo.goods_sku_id == v.goods_sku_id
					) {
						v.total_num = v.total_num * 1 + 1;
						break;
					}
				}
			}
		}
	},
	// 单个商品 数量减少(默认减少1)
	[GOODS_NUM_SUB1](state, checkInfo) {
		console.log("单个商品 数量减少(默认减少1)", checkInfo);
		for (let item of state.userCarts1) {
			if (item.id == checkInfo.id) {
				for (let v of item.goods_list) {
					if (
						checkInfo.goods_id == v.goods_id &&
						checkInfo.goods_sku_id == v.goods_sku_id
					) {
						v.total_num = v.total_num * 1 - 1;
						break;
					}
				}
			}
		}
	},
	// 保存货币价格列表
	[SET_TOKEN_PRICE](state, data) {
		state.tokenPriceList = data
	},
	[SET_WALLET_ADDRESS](state, data) {
		uni.setStorageSync("walletAddress", data)
		state.walletAddress = data
	},
	[SET_WALLET_PRIVATEKEY](state, data) {
		uni.setStorageSync("walletPrivatekey", data)
		state.walletPrivateKey = data
	},
	[SET_NFT_CONFIG](state, data) {
		uni.setStorageSync("nftConfig", data)
		state.nftConfig = data
	}
};